import './Mars3dSetUp.scss'
import { v4 } from 'uuid'
import { Button } from 'antd'
import { useState } from 'react'
import { useMount } from 'ahooks'
import PropTypes from 'prop-types'

import { UseConfigSection } from '../../Template/FormTemplate'
import { optionsList, elementMapping } from '../../Assets/Params'
const Mars3dAddEle = ({ data, ModalEvent, Off }) => {
    Mars3dAddEle.propTypes = { data: PropTypes.object, ModalEvent: PropTypes.object, Off: PropTypes.func }

    const [Uuid, setUuid] = useState()
    const [List, setList] = useState()
    const [Data, setData] = useState()

    useMount(() => {
        const uuid = v4()
        const { obj, gro } = data
        const sel = Object.keys(gro).map(key => gro[key]).sort((a, b) => a.sort - b.sort).map(item => ({ label: item.label, value: item.key }))
        obj[uuid] = { isEnable: true, uuid }
        setData(obj)
        setUuid(uuid)
        setList([
            { key: 101, uuid, label: '元素名称', type: 'InputTemp', name: 'title' },
            { key: 102, uuid, label: '元素分组', type: 'SelectTemp', name: 'groKey', options: sel },
            { key: 103, uuid, label: '元素排序', type: 'InputNumberTemp', name: 'sortId' },
            { key: 104, uuid, label: '是否启用', type: 'RadioTemp', name: 'isEnable', options: optionsList.isEnable },
            { key: 105, uuid, label: '元素类型', type: 'TreeSelectTemp', name: 'type', options: optionsList.vectorData },
        ])
    })

    const onSubmit = () => {
        const key = Data[Uuid].groKey
        delete Data[Uuid].groKey
        Data[Uuid] = { ...Data[Uuid], ...elementMapping[Data[Uuid].type].defaultData }
        ModalEvent.emit({ type: 'addDetails', data: { obj: Data, uuid: Uuid, key } })
        Off()
    }

    const onClose = () => {
        Off()
        // delete data[Uuid]
    }

    return (
        <div className='Mars3dSetUp Mars3dAddEle'>
            <div className='FormList'>
                {Uuid && List && UseConfigSection(Data, List)}
            </div>
            <div className='Button'>
                <Button type='primary' danger onClick={onClose}>取消</Button>
                <Button type='primary' onClick={onSubmit}>保存</Button>
            </div>
        </div>
    )
};

export default Mars3dAddEle;